package cn.sdormitory.service.impl;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.vo.BClassVo;
import cn.sdormitory.common.utils.DateTimeUtils;
import cn.sdormitory.dormitoryHygiene.vo.AppDorHygieneCountVO;
import cn.sdormitory.retention.AppRetentionCountVO;
import cn.sdormitory.retention.DormitoryRetention;
import cn.sdormitory.retention.dao.RetentionDao;
import cn.sdormitory.retention.RetentionVO;
import cn.sdormitory.service.AppRetentionService;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author : yy
 * @date: 2024/9/11 11:23
 * @description:
 */
@Service
public class AppRetentionServiceImpl extends ServiceImpl<RetentionDao, DormitoryRetention> implements AppRetentionService {
    @Resource
    RetentionDao retentionDao;

    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private BClassDao classDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private BStudentDao bStudentDao;
    @Resource
    private SysDeptDao sysDeptDao;

    @Override
    public List<RetentionVO> listTodayRetention() {
        return retentionDao.listTodayRetention();
    }

    @Override
    public void retentionRegister(Long studentId, Long classId,String registrantUser) {
        LambdaQueryWrapper<DormitoryRetention> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DormitoryRetention::getStudentId,studentId);
        queryWrapper.apply("DATE(registrant_date) = CURDATE()");
        DormitoryRetention dormitoryRetention = retentionDao.selectOne(queryWrapper);

        if (!Optional.ofNullable(dormitoryRetention).isPresent()) {
            DormitoryRetention retention = new DormitoryRetention();
            retention.setStudentId(studentId);
            retention.setClassId(classId);
            retention.setRegistrantUser(registrantUser);
            retention.setRegistrantDate(new Date());
            retentionDao.insert(retention);
        }
    }

    @Override
    public void delRetention(Long id) {
        retentionDao.deleteById(id);
    }

    @Override
    public List<AppRetentionCountVO> getAppRetentionCount(Long userId, String rq) {
        boolean isDeptManager = false ; //是否部门经理
        List<Long> roleIdList = sysUserRoleService.listRoleIdByUserId(userId);
        for (Long roleId : roleIdList) {
            if(roleId == 2 || roleId == 1) {
                isDeptManager = true ;
                break;
            }
        }
        List<BClassVo> classList = new ArrayList<>();
        if(isDeptManager) {
            SysUser user = sysUserDao.selectById(userId);
            List<Long> deptIds = sysDeptDao.getAllChildByParent(user.getDeptId());
            //classList = classDao.getAppClassDeptByClaId(user.getDeptId());
            classList = classDao.getAppClassByDeptIds(deptIds);
        }else {
            List<BClassVo> cList = classDao.getAppClassListByTeaId(userId);
            for (BClassVo bClassVo : cList) {
                LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BStudent::getClassId, bClassVo.getClassId())
                        .eq(BStudent::getIsGraduated, "0");
                List<BStudent> studentList = bStudentDao.selectList(wrapper);
                if (!studentList.isEmpty()) {
                    classList.add(bClassVo);
                }
            }
        }

        return classList.parallelStream()
                .map(classVo -> {
                    Long classId = classVo.getClassId();
                    AppRetentionCountVO countVO = new AppRetentionCountVO();
                    countVO.setClassName(classVo.getClassName());
                    countVO.setClassId(classId);

                    Long classNum = classDao.getStuCountByClaId(classId);
                    countVO.setClassNum(classNum);

                    Integer liveNum = classDao.getBoardingStuCountByClassId(classId.toString());
                    countVO.setLiveNum(liveNum);

                    LambdaQueryWrapper<DormitoryRetention> retentionWrapper = new LambdaQueryWrapper<>();
                    retentionWrapper.eq(DormitoryRetention::getClassId,classId);
                    retentionWrapper.eq(DormitoryRetention::getRegistrantDate, DateTimeUtils.strToDate(rq));
                    Integer count = retentionDao.selectCount(retentionWrapper);
                    countVO.setRetentionNum(count);

                    return countVO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<RetentionVO> getRetentionStuByClassId(Long classId, String rq) {
        return retentionDao.getRetentionStuByClassId(classId,rq);
    }
    
    @Override
    public List<DormitoryRetention> getMonthlyStatisticsRetention(List<Long> classIds, String accessDate) {
        return retentionDao.getMonthlyRetention(classIds, accessDate);
    }
}
